package com.sec.android.app.myfiles.launch;

import android.app.Activity;
import android.content.Intent;
import android.util.SparseArray;

import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.info.AppFeatures;
import com.sec.android.app.myfiles.navigation.NavigationManager;
import com.sec.android.app.myfiles.util.chain.ChainBuilder;

public class LaunchMgr {
    private static SparseArray<LaunchMgr> mInstanceMap = new SparseArray<>();

    private AbsHandleLaunchImp mHandleStartImp;

    public static LaunchMgr getInstance(int processId) {
        LaunchMgr instance = mInstanceMap.get(processId);
        if (instance == null) {
            instance = new LaunchMgr();
            mInstanceMap.put(processId, instance);
        }
        return instance;
    }

    private LaunchMgr() {
        ChainBuilder<AbsHandleLaunchImp> builder = new ChainBuilder<>().
                append(new HandleStartFolderImp()).
                append(new HandleCompressFileImp()).
                append(new HandleSFinderImp()).
                append(new HandleDownloadHistoryImp()).
                append(new HandlePickerImp()).
                append(new HandleSearchImp()).
                append(new HandlePathPickerImp()).
                append(new HandleViewCategoryImp()).
                append(new HandleCreateDocumentImp()).
                append(new HandleExecutableRecentImp()).
                append(new HandleRunOptimizeStorageImp());
        mHandleStartImp = builder.getInstance();
    }

    public void handleStart(int processId, Activity activity, Intent intent) {
        if (!mHandleStartImp.handleStart(processId, activity, intent)) {
            NavigationManager navigationManager = NavigationManager.getInstance(processId);
            // if there is no current info, it is first start of My Files.
            // else, do nothing and resume previous info.
            if (navigationManager.getCurInfo() == null) {
                if (AppFeatures.isNoFocusTabletUI()) {
                    MyFilesFacade.goHomeBlank(processId, null);
                } else {
                    MyFilesFacade.goHome(processId, null, activity);
                }
            }
        }
    }
}
